package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Dto.CommunityDto;
import com.xiangmo.travel.Dvo.CommunityDvo;
import com.xiangmo.travel.Entity.Community;
import com.xiangmo.travel.Entity.CommunityComments;
import com.xiangmo.travel.Entity.CommunityLike;
import com.xiangmo.travel.Entity.User;
import com.xiangmo.travel.Result.PageToListResult;
import com.xiangmo.travel.Service.CommunityService;
import com.xiangmo.travel.mapper.CommunityCommentsMapper;
import com.xiangmo.travel.mapper.CommunityLikeMapper;
import com.xiangmo.travel.mapper.CommunityMapper;
import com.xiangmo.travel.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author a1304
* @description 针对表【community】的数据库操作Service实现
* @createDate 2023-03-19 23:37:40
*/
@Service
public class CommunityServiceImpl  implements CommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommunityLikeMapper communityLikeMapper;
    @Autowired
    private CommunityCommentsMapper communityCommentsMapper;
    //根据类型查询社区数据 分页 可以考虑对数据进行封装
    @Override
    public PageToListResult<CommunityDvo> getCommunityByPage(int pageNum, int pageSize, String type) {
        Page<Community> page = new Page<>(pageNum, pageSize);
        communityMapper.selectAllByType(page, type);
        List<Community> list = page.getRecords();
        List<CommunityDvo> communityDvoList = new ArrayList<>();
        for(Community c : list){
            CommunityDvo communityDvo = new CommunityDvo();
            User user = userMapper.selectById(c.getUserId());
            LambdaQueryWrapper<CommunityLike> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(CommunityLike::getCommunityId, c.getId());
            Long likeNum = communityLikeMapper.selectCount(lambdaQueryWrappers);
            communityDvo.setCommunityId(c.getId());
            communityDvo.setUserId(c.getUserId());
            communityDvo.setName(user.getName());
            communityDvo.setUserIcon(user.getImage());
            communityDvo.setUserSex(user.getSex());
            communityDvo.setType(c.getType());
            communityDvo.setTitle(c.getTitle());
            communityDvo.setImage(c.getImage());
            communityDvo.setContent(c.getContent());
            communityDvo.setCreatedAt(c.getCreatedAt());
            communityDvo.setLikeNum(likeNum);
            LambdaQueryWrapper<CommunityComments> lambdaQueryWrappert = new LambdaQueryWrapper<>();
            lambdaQueryWrappert.eq(CommunityComments::getCommunityId, c.getId());
            Long commentNum = communityCommentsMapper.selectCount(lambdaQueryWrappert);
            communityDvo.setCommentNum(commentNum);
            communityDvoList.add(communityDvo);
        }
        PageToListResult<CommunityDvo> pageToListResult = new PageToListResult();
        pageToListResult.setPages(page.getPages());
        pageToListResult.setList(communityDvoList);
        pageToListResult.setPageCurrent(page.getCurrent());
        pageToListResult.setPageTotal(page.getTotal());
        pageToListResult.setSize(page.getSize());
        return pageToListResult;
    }

    @Override
    public PageToListResult<CommunityDvo> getCommunityByPageAndUserId(Long userId, int pageNum, int pageSize, String type) {
        Page<Community> page = new Page<>(pageNum, pageSize);
        communityMapper.selectAllByType(page, type);
        List<Community> list = page.getRecords();
        List<CommunityDvo> communityDvoList = new ArrayList<>();
        //通过用户id返回该用户是否点赞过

        for(Community c : list){
            CommunityDvo communityDvo = new CommunityDvo();
            User user = userMapper.selectById(c.getUserId());
            int isChoose = 0;
            if(communityLikeMapper.selectByUserIdAndCommunityId(userId, c.getId()).size() != 0){
                isChoose = 1;
            }
            LambdaQueryWrapper<CommunityLike> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(CommunityLike::getCommunityId, c.getId());
            Long likeNum = communityLikeMapper.selectCount(lambdaQueryWrappers);
            communityDvo.setCommunityId(c.getId());
            communityDvo.setIsChoose(isChoose);
            communityDvo.setUserId(c.getUserId());
            communityDvo.setName(user.getName());
            communityDvo.setUserIcon(user.getImage());
            communityDvo.setUserSex(user.getSex());
            communityDvo.setType(c.getType());
            communityDvo.setTitle(c.getTitle());
            communityDvo.setImage(c.getImage());
            communityDvo.setContent(c.getContent());
            communityDvo.setCreatedAt(c.getCreatedAt());
            communityDvo.setLikeNum(likeNum);
            //查询评论数量
            LambdaQueryWrapper<CommunityComments> lambdaQueryWrappert = new LambdaQueryWrapper<>();
            lambdaQueryWrappert.eq(CommunityComments::getCommunityId, c.getId());
            Long commentNum = communityCommentsMapper.selectCount(lambdaQueryWrappert);
            communityDvo.setCommentNum(commentNum);
            communityDvoList.add(communityDvo);
        }
        PageToListResult<CommunityDvo> pageToListResult = new PageToListResult();
        pageToListResult.setPages(page.getPages());
        pageToListResult.setList(communityDvoList);
        pageToListResult.setPageCurrent(page.getCurrent());
        pageToListResult.setPageTotal(page.getTotal());
        pageToListResult.setSize(page.getSize());
        return pageToListResult;
    }

    //发布指定社区的一个文章
    @Override
    public int setOneCommunity(CommunityDto communityDto) {
        Community community = Community.builder()
                .content(communityDto.getContent())
                .type(communityDto.getType().getPoints())
                .image(communityDto.getImage())
                .title(communityDto.getTitle())
                .userId(communityDto.getUserId())
                .build();
        return communityMapper.insert(community);
    }
    //获取当前指定社区的总评论人数，也就是热度
    @Override
    public Long getCommunityNum(String type) {
        LambdaQueryWrapper<Community> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Community::getType, type);
        return communityMapper.selectCount(lambdaQueryWrapper);
    }
    //根据文章id查询文章的详情信息
    @Override
    public CommunityDvo getCommunityById(Long id) {
        Community community = communityMapper.selectById(id);
        CommunityDvo communityDvo = new CommunityDvo();
        User user = userMapper.selectById(community.getUserId());
        communityDvo.setCommunityId(community.getId());
        communityDvo.setUserId(user.getId());
        communityDvo.setName(user.getName());
        communityDvo.setUserIcon(user.getImage());
        communityDvo.setType(community.getType());
        communityDvo.setUserSex(user.getSex());
        communityDvo.setTitle(community.getTitle());
        communityDvo.setContent(community.getContent());
        communityDvo.setImage(community.getImage());
        communityDvo.setCreatedAt(community.getCreatedAt());
        return communityDvo;
    }
    //根据用户id查询该用户发过的帖子
    @Override
    public Page<Community> getCommunityUserByPage(int pageNum, int pageSize, Long id) {
        Page<Community> page = new Page<>(pageNum, pageSize);
        return communityMapper.selectAllByUserId(page, id);
    }

    @Override
    public int deleteCommunityById(Long id) {
        return communityMapper.deleteById(id);
    }
}
